﻿using System;

namespace Ehai.Web.Framework.Rules
{
    class DateRuleHandle : IMsgRuleHandle
    {
        #region IMsgRuleHandle 成员

        public const string Name = "date";

#if DEBUG

        private int _execStatus = 0;
        public int ExecStatus
        {
            get { return _execStatus; }
        }

#endif

        /// <summary>
        /// 系统当前时间
        /// </summary>
        /// <param name="p">
        /// 传入数组<br />
        /// 0:(必选)处理类型,date<br />
        /// </param>
        /// <param name="ci">环境变量</param>
        /// <example>
        /// string[] p = new string[1] { "date" };
        /// string[][] t = DateRule(p, "zh");
        /// </example>
        /// <exception cref="Ehai.Web.Framework.WebException">
        /// Ehai.Web.Framework.WebException:时间读取错误！
        /// </exception>
        /// <returns>返回指定语言的系统当前时间</returns> 
        public string[][] Execute(string[] p, ContextInfo ci)
        {
            try
            {
                System.DateTime date = System.DateTime.Now;
                string da = String.Empty;
                switch (ci.GetLanguage().ToString().ToLower())
                {
                    case "zh":
                        da = date.ToString("yyyy\\/MM\\/dd HH:mm dddd");//中文
                        break;
                    case "en":
                        da = date.ToString("yyyy\\/MM\\/dd HH:mm") + " " + date.DayOfWeek;//English
                        break;
                    default:
                        break;
                }

#if DEBUG
                // 操作成功,状态值为0
                _execStatus = 0;
#endif
                return new string[1][] { new string[] { da } };

            }
            catch (Exception ex)
            {

#if DEBUG
                // 操作失败并捕获异常,状态值为1
                _execStatus = 1;
#endif
                throw new Ehai.Web.Framework.WebException("Ehai.WebFramework.DateRule", "时间读取错误！", ex);

            }
        }

        public string GetHelpInfo()
        {
            return "系统当前时间";
        }

        public string GetHelpDetail()
        {
            return  "输入要显示的语言类型，返回系统当前时间:<br />" +
                    "例: { date,zh }; //返回正确结果<br />" +
                    "返回:指定语言的系统当前时间";
        }

        public string[] GetParametersInfo()
        {
            string[] t = new string[1];
            t[0] = "(必选)处理类型,即\"date\"";
            return t;
        }

        #endregion
    }
}
